package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.aistar.entity.Register;
import tech.aistar.entity.Result;
import tech.aistar.entity.Tenant;
import tech.aistar.service.IRegisterService;
import tech.aistar.service.ITenantService;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;


@Controller
@RequestMapping("/Tenant")
public class TenantCentre {
    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IRegisterService registerService;

    @GetMapping("/info")
    public String select(HttpServletRequest request) {
        String tenantId = request.getParameter("tenantId");
        Tenant tenant = tenantService.selectById(Integer.valueOf(tenantId));
        request.setAttribute("tenant", tenant);
        Register register = registerService.selectById(tenant.getRegisterId());
        request.setAttribute("register", register);
        return "/personal_homepage/tenant/info";
    }

    @PostMapping("/changepwd")
    public @ResponseBody
    Result updatepwd(Integer id, String password) {
        int count = registerService.updateById(id, password);

        if (count == 1) {
            return new Result("200", "修改成功", registerService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }


    @PostMapping("/changephone")
    public @ResponseBody
    Result updatephone(String phone, Integer id) {
        int count = tenantService.updateById(phone, id);

        if (count == 1) {
            Result result = new Result("200", "修改成功", tenantService.selectById(id));
            return result;
        } else {
            Result result = new Result("500", "修改失败");
            return result;
        }
    }


    @PostMapping("/changename")
    public @ResponseBody
    Result updatename(String name, Integer id) {

        int count = tenantService.updateNameById(name, id);

        if (count == 1) {
            return new Result("200", "修改成功", tenantService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }


    @PostMapping("/changegender")
    public @ResponseBody
    Result updategender(String gender, Integer id) {

        int count = tenantService.updategenderById(gender, id);

        if (count == 1) {
            return new Result("200", "修改成功", tenantService.selectById(id));
        } else {
            return new Result("500", "修改失败");
        }
    }


    @PostMapping("/insertIdentity")
    public @ResponseBody Result insertIdentity(String identity,Integer id){

        int count = tenantService.updateIdentityById(identity, id);

        if(count==1){
            return new Result("200","修改成功",tenantService.selectById(id));
        }else{
            return new Result("500","修改失败");
        }
    }


    @GetMapping("/Pic")
    public String Pic(HttpServletRequest request) {
        return "/personal_homepage/Pic";
    }


    @PostMapping("/lay")
    public @ResponseBody Result uploads(HttpServletRequest req,@RequestParam("file") MultipartFile file){
        //重新进行命名
        String fileName = UUID.randomUUID().toString()+"_"+file.getOriginalFilename();

        //构建File实例
        File f = new File("src/main/webapp/img/"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new Result("200","文件上传成功!",fileName);
    }




}


